package org.microblog.service.impl;

import org.apache.ibatis.session.SqlSession;
import org.microblog.dao.BlogMapper;
import org.microblog.dao.CommentMapper;
import org.microblog.entity.Comment;
import org.microblog.service.CommentService;
import org.microblog.util.DatabaseUtil;
import org.microblog.util.MyBatisUtil;
import org.microblog.util.Page;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CommentServiceImpl implements CommentService {
    SqlSession sqlSession = MyBatisUtil.createSqlSession();
    BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
    CommentMapper  commentMapper = sqlSession.getMapper(CommentMapper.class);
    @Override
    public int getCommentCountByBid(int bid) throws SQLException {
        Connection conn = null;
        try{
            return commentMapper.getCommentCountByBid(bid);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public List<Comment> getCommentsByBid(int bid) throws SQLException {
        Connection conn = null;
        try{
            return commentMapper.getCommentsByBid(bid);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

    @Override
    public int addComment(Comment comment) throws SQLException {
        Connection conn = null;
        int result;
        try {
            result = commentMapper.addComment(comment);
            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            if (conn != null)
                try {
                    conn.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            throw e;
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    @Override
    public int deleteCommentsByBid(int bid) throws SQLException {
        Connection conn = null;
        int result;
        try {
            result = commentMapper.deleteCommentsByBid(bid);

            sqlSession.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            if (conn != null)
                try {
                    conn.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            throw e;
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }

    @Override
    public void getCommentsByPage(int bid, Page pageObj) throws SQLException {
        Connection conn = null;
        try{
            int totalCount = commentMapper.getCommentCountByBid(bid);
            pageObj.setTotalCount(totalCount); // 设置总数量并计算总页数
            if (totalCount > 0) {
                // 对末页进行控制
                if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                    pageObj.setCurrPageNo(pageObj.getTotalPageCount());
                // 进行分页查询
                List<Comment> commentsList = commentMapper.getCommentsByPage(bid,pageObj.getCurrPageNo(),pageObj.getPageSize());
                pageObj.setCommentList(commentsList);
            } else {
                pageObj.setCurrPageNo(0);
                pageObj.setCommentList(new ArrayList<Comment>());
            }
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            MyBatisUtil.closeSqlSession(sqlSession);
        }
    }

}
